package com.example.zhaohang.employmentapp;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.zhaohang.been.ForgNumberBeen;
import com.example.zhaohang.contect.UriContect;
import com.example.zhaohang.utils.GsonUtil;
import com.example.zhaohang.utils.ToastMake;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.MediaType;

public class ChengPawTwoActivity extends BasicActivity {

    private Button mSubmit;
    private EditText mNewPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView_V(R.layout.activity_cheng_paw_two);
        setLeftShow(1, R.mipmap.back);
        getLift_V().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setTitle_V("更换密码");

        mNewPwd = (EditText) findViewById(R.id.name3);
        mSubmit = (Button) findViewById(R.id.name8);

        mSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isConnected(ChengPawTwoActivity.this)) {
                    submit();
                }
            }
        });
    }

    private void submit() {
        String trim = mNewPwd.getText().toString().trim();
        if (TextUtils.isEmpty(trim)){
            ToastMake.showToast("密码不能为空");
            return;
        }
        if (trim.length()<6||trim.length()>25){
            ToastMake.showToast("请输入6-25位长度的密码");
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put("phone", MyApplayCation.phone);
        map.put("pwd", trim);
        map.put("code", MyApplayCation.registCode);
        final String json = GsonUtil.parseMapToJson(map);
        showProgrees("修改中,请稍等.....");
        new Thread(new Runnable() {
            @Override
            public void run() {

                OkHttpUtils
                        .postString()
                        .url(UriContect.PASSWORD)
                        .content(json)
                        .mediaType(MediaType.parse("application/json; charset=utf-8"))
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {
                                ToastMake.showToast(getResources().getString(R.string.name18));
                                hideProgree();
                            }

                            @Override
                            public void onResponse(String response, int id) {
                                hideProgree();
                                ForgNumberBeen bean = GsonUtil.parseJsonToBean(response, ForgNumberBeen.class);
                                if (bean == null) {
                                    ToastMake.showToast(getResources().getString(R.string.name18));

                                    return;
                                }
                                if (!TextUtils.equals(bean.getStatus(),"1")){
                                    ToastMake.showToast(bean.getMsg());
                                    return;
                                }else {
                                    ToastMake.showToast(bean.getMsg());
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            skipOtherActivity(SettingActivity.class,true);
                                            finish();
                                        }
                                    });
                                }

                            }
                        });
            }
        }).start();
    }
}
